//
//  GYDSendWBViewController.swift
//  Weibo
//
//  Created by 贵永冬 on 16/8/1.
//  Copyright © 2016年 贵永冬. All rights reserved.
//

import UIKit
import ReactiveCocoa

class GYDSendWBViewController: UIViewController {

    static func showWithVC(vc:UIViewController) {
        let sendWBVC = GYDSendWBViewController()
        let nav = GYDNavigationController(rootViewController: sendWBVC)
        vc.presentViewController(nav, animated: true, completion: nil)
    }
    
    
    var sendButton : UIButton?
    var sendWBView : GYDSendWBView?
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.view.backgroundColor = UIColor.whiteColor()
        setUpNavigationBar()
        createSendWBView()
    }
    func setUpNavigationBar() {
        self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: "取消", style: UIBarButtonItemStyle.Plain, target: self, action: #selector(GYDSendWBViewController.cancelButtonClick))
        let sendButton = UIButton(type: UIButtonType.Custom)
        sendButton.frame = CGRect(x: 0, y: 0, width: 50, height: 30)
        sendButton.setBackgroundImage(UIImage.resizedImage("common_button_big_white"), forState: UIControlState.Normal)
        sendButton.setBackgroundImage(UIImage.resizedImage("common_button_big_white"), forState: UIControlState.Highlighted)
        sendButton.setBackgroundImage(UIImage.resizedImage("radar_card_button_discount"), forState: UIControlState.Selected)
        sendButton.setTitle("发送", forState: UIControlState.Normal)
        sendButton.setTitleColor(RGB(180, g: 180, b: 180), forState: UIControlState.Normal)
        sendButton.setTitleColor(UIColor.whiteColor(), forState: UIControlState.Selected)
        sendButton.titleLabel?.font = UIFont.systemFontOfSize(14)
        self.sendButton = sendButton
        sendButton.rac_signalForControlEvents(UIControlEvents.TouchUpInside).subscribeNext { [unowned self] (x) in
            self.sendButtonClick(x)
        }
        self.navigationItem.rightBarButtonItem = UIBarButtonItem(customView: sendButton)
        
        
        let titleView = UIView(frame: CGRect(x: 0, y: 0, width: GYDWindowWidth - 150, height: 44))
        
        let descLabel = UILabel(frame: CGRect(x: 0, y: 6, width: titleView.width, height: 15))
        descLabel.text = "发微博"
        descLabel.font = UIFont.boldSystemFontOfSize(16)
        descLabel.textColor = NAVTINTCOLOR
        descLabel.textAlignment = NSTextAlignment.Center
        titleView.addSubview(descLabel)
        
        let nameLabel = UILabel(frame: CGRect(x: 0, y: 23, width: titleView.width, height: 15))
        nameLabel.text = GYDUserModel.sharedInstance.userInfo?.screen_name
        nameLabel.font = UIFont.systemFontOfSize(14)
        nameLabel.textColor = RGB(150, g: 150, b: 150)
        nameLabel.textAlignment = NSTextAlignment.Center
        titleView.addSubview(nameLabel)
        
        self.navigationItem.titleView = titleView
        
    }
    @objc private func cancelButtonClick() {
        self.view.endEditing(true)
        self.navigationController?.dismissViewControllerAnimated(true, completion: nil)
    }
    private func sendButtonClick(obj:AnyObject){
        if sendButton?.selected == false {
            true
        }
        print("send")
    }
    private func createSendWBView() {
        let sendWBView = GYDSendWBView(frame: CGRect(x: 0, y: 64, width: GYDWindowWidth, height: GYDWindowHeight - 64))
        self.sendWBView = sendWBView
        sendWBView.textView?.rac_textSignal().subscribeNext({ [unowned self] (x) in
            guard let text : String = x as? String else {
                return
            }
            self.sendButton?.selected = text.characters.count > 0
        })
        view.addSubview(sendWBView)
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewDidAppear(animated)
        self.sendWBView?.textView?.becomeFirstResponder()
    }

    
    deinit {
        print("发送微博消失000")
    }
    
}
